package _interview100;

import _object.TreeNode;

import java.util.ArrayDeque;
import java.util.Queue;

/**
 * 230. 二叉搜索树中第K小的元素
 */
public class No230 {
    private Queue<Integer> queue;

    public int kthSmallest(TreeNode root, int k) {
        queue = new ArrayDeque<>();
        dfs(root);
        for (int i = 1; i < k; i++) {
            queue.poll();
        }
        return queue.poll();
    }

    private void dfs(TreeNode node) {
        if (node == null) return;
        dfs(node.left);
        queue.add(node.val);
        dfs(node.right);
    }
}
